package com.zhejiangzhengyuan.municipal_common.utils.LED;

import com.zhejiangzhengyuan.municipal_common.mapper.InspectionTasksMapper;
import com.zhejiangzhengyuan.municipal_common.service.CaseService;
import com.zhejiangzhengyuan.municipal_common.service.PumpEstimatedValueService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.DateFormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@EnableScheduling
@Slf4j
public class ScheduleTaskUtils {

    @Autowired
    public URLUtils urlUtils;
    @Autowired
    public InspectionTasksMapper inspectionTasksMapper;
    @Autowired
    public CaseService caseService;
    @Autowired
    public PumpEstimatedValueService pumpEstimatedValueService;


    /**
     * 定时重启
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Scheduled(cron = "0 0 00,12 * * ? ") //todo 每天重启执行时间  凌晨0点和12点
    public void ScheduleToRestart() throws IOException, InterruptedException {

        log.info("道路积水系统开始定时重启,现在时间是:" + LocalDateTime.now());
        Document document = Jsoup.connect(urlUtils.GetDeviceMainURL()).get();//todo 得到在线的设备
        List<Map<String, String>> mapList = DeviceUtils.GetDeviceData(document);
        for (Map<String, String> map : mapList) {
            String mac = map.get("MAC");
            String deviceCode = map.get("DeviceCode");
            Thread.sleep(10000); //todo 延迟10秒
            ToRestart(deviceCode); //todo 开启异步
        }
    }


    /**
     *
     * 定时周期巡检
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Scheduled(cron = "0 0 07 * * ? ")
    public void restartInspectionTask() throws IOException, InterruptedException {
        String taskTime = DateFormatUtil.getStringByTime("yyyy-MM-dd", System.currentTimeMillis()+1000*60*60*24);
        //更新次日巡检任务状态
        int todayCycleTasks = inspectionTasksMapper.findTodayCycleTasks(taskTime);
        if (todayCycleTasks > 0) {
            caseService.insertTomorrowMessageToDepartment(ConstantUtil.MODULE_NAME.WAY, "周期巡检任务更新", "领取任务", taskTime,"路桥巡检组");
        }
    }
    @Async
    public void ToRestart(String deviceCode) {
        log.info("LED显示屏 设备编号为：" + deviceCode + " 准备重启！");
        Map<String, String> map = new HashMap<>();
        map.put("devCode", deviceCode);
        map.put("content", "");
        map.put("type", "50");//todo 50指令为指令重启
        HttpClientUtils.doPost(urlUtils.GetSendTaskURL(), map);
    }

    //定時查詢泵站
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void selectPumpEstimatedValue(){
        //液位
        pumpEstimatedValueService.selectPumpEstimatedValue();
        //气体
        pumpEstimatedValueService.selectPumpGasValue();
        //电流
        pumpEstimatedValueService.selectPumpElectricityValue();

    }


    /**
     * 定时 对时
     *
     * @throws IOException
     * @throws InterruptedException
     */
    @Scheduled(cron = "0 0 10,16,23 * * ? ") //todo 每天定时执行时间  早上10点和16点和23点
    public void ScheduleTimeHack() throws IOException, InterruptedException {

        log.info("道路积水系统开始定时重启,现在时间是:" + LocalDateTime.now());
        Document document = Jsoup.connect(urlUtils.GetDeviceMainURL()).get();//todo 得到在线的设备
        List<Map<String, String>> mapList = DeviceUtils.GetDeviceData(document);
        for (Map<String, String> map : mapList) {
            String mac = map.get("MAC");
            String deviceCode = map.get("DeviceCode");
            Thread.sleep(10000); //todo 延迟10秒
            TimeHack(deviceCode); //todo 开启异步
        }
    }

    @Async
    public void TimeHack(String deviceCode) {
        log.info("LED显示屏 设备编号为：" + deviceCode + " 准备对时！");
        Map<String, String> map = new HashMap<>();
        map.put("devCode", deviceCode);
        map.put("content", "");
        map.put("type", "80");//todo 80指令为同步时钟
        HttpClientUtils.doPost(urlUtils.GetSendTaskURL(), map);
    }

}
